package com.xq.springboot.service;

import com.xq.springboot.exception.GirlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xq.springboot.dao.jpa.GirlRepository;
import com.xq.springboot.domain.Girl;
import com.xq.springboot.enums.ResultEnum;

/**
 * 测试事务的服务类.
 * 
 * @author 郭凯强
 */
@Service
public class GirlService {
	@Autowired
	private GirlRepository girlRepository;

	@Transactional
	public void insertTwo() {
		Girl girlA = new Girl();
		girlA.setCupSize("A");
		girlA.setAge(18);
		girlRepository.save(girlA);

		Girl girlB = new Girl();
		girlB.setCupSize("BBBB");
		girlB.setAge(19);
		girlRepository.save(girlB);
	}
	
	/**
	 * 测试@ControllerAdvice类的方法.
	 * @param id 主键
	 * @throws Exception 抛出的异常
	 */
	public void getAge(Integer id) throws Exception{
        Girl girl = girlRepository.findById(id).get();
        Integer age = girl.getAge();
        if (age < 10) {
            //返回"你还在上小学吧" code=100
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        }else if (age > 10 && age < 16) {
            //返回"你可能在上初中" code=101
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }

        //如果>16岁,加钱
        //...
    }
	
	/**
     * 通过Id查询一个女生的信息, 仅供测试使用
     * @param id
     * @return
     */
    public Girl findOne(Integer id) {
        return girlRepository.findById(id).get();
    }
}
